package com.shengsheng.expschedule.service.ucenter.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shengsheng.expschedule.base.result.R;
import com.shengsheng.expschedule.base.util.JwtUtils;
import com.shengsheng.expschedule.service.ucenter.entity.Teacher;
import com.shengsheng.expschedule.service.ucenter.entity.Vo.TeacherQueryVo;
import com.shengsheng.expschedule.service.ucenter.entity.Vo.TeacherVo;
import com.shengsheng.expschedule.service.ucenter.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author yuan
 * @since 2021-11-04
 */
@Api(tags = "教师管理")
@RestController
@RequestMapping("/admin/ucenter/teacher")
@RefreshScope
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @ApiOperation("新增用户")
    @PostMapping("/save")
    public R save(@ApiParam(value = "会员对象", required = true)
                  @RequestBody Teacher teacher) {
        boolean flag = teacherService.saveMemberByAdmin(teacher);
        if (flag) return R.ok().message("添加成功");
        return R.error().message("添加失败");
    }

    @ApiOperation("注销")
    @PostMapping("/exit/{teacherId}")
    public R exit(@ApiParam(value = "教师工号", required = true)
                  @PathVariable String teacherId) {
        if (teacherService.exit(teacherId)) return R.ok().message("注销成功");
        return R.error().message("注销失败");
    }

    @ApiOperation("根据教师工号查询该教师拥有的所有权限信息")
    @GetMapping("/getRightList/{teacherId}")
    public R getRightList(@ApiParam(value = "教师工号", required = true)
                          @PathVariable String teacherId) {
        return R.ok()
                .data("rightList", teacherService.getRightListByTeacherId(teacherId))
                .message("获取权限成功");
    }

    @ApiOperation("根据教师工号查询查询该教师所有的菜单权限信息")
    @GetMapping("/getRightMenuList/{teacherId}")
    public R getRightMenuList(@ApiParam(value = "教师工号", required = true)
                              @PathVariable String teacherId) {
        return R.ok()
                .data("rightMenuList", teacherService.getRightMenuListByTeacherId(teacherId))
                .message("获取权限成功");
    }

    @ApiOperation("根据教师工号查询教师姓名")
    @GetMapping("/getUserName")
    public R getUserName(HttpServletRequest request) {
        return R.ok()
                .data("username", JwtUtils.getJWTInfoByRequest(request).getUsername());
    }

    @ApiOperation("查询所有教师信息")
    @GetMapping("/getTeacherList")
    public R getTeacherList() {
        return R.ok()
                .data("teacherList", teacherService.getTeacherVoList())
                .message("获取数据成功");
    }

    @ApiOperation("根据教师 id 查询教师信息")
    @GetMapping("/getTeacher/{teacherId}")
    public R getTeacher(@ApiParam(value = "教师 id", required = true)
                        @PathVariable String teacherId) {
        return R.ok()
                .data("teacher", teacherService.getTeacherVoById(teacherId))
                .message("获取数据成功");
    }

    @ApiOperation("更新教师信息")
    @PostMapping("/updateTeacher")
    public R updateTeacher(@ApiParam(value = "Teacher 对象", required = true)
                           @RequestBody TeacherVo teacherVo) {
        if (teacherService.updateTeacher(teacherVo)) return R.ok().message("更新成功");
        return R.error().message("更新失败");
    }

    @ApiOperation("根据教师 id 禁用用户")
    @PostMapping("/disableTeacher/{teacherId}")
    public R disableTeacher(@ApiParam(value = "教师 id", required = true)
                            @PathVariable String teacherId) {
        if (teacherService.disableTeacher(teacherId)) return R.ok().message("禁用成功");
        return R.error().message("禁用失败");
    }

    @ApiOperation("根据教师 id 解禁用户")
    @PostMapping("/enableTeacher/{teacherId}")
    public R enableTeacher(@ApiParam(value = "教师 id", required = true)
                           @PathVariable String teacherId) {
        if (teacherService.enableTeacher(teacherId)) return R.ok().message("解禁成功");
        return R.error().message("解禁失败");
    }

    @ApiOperation("删除教师")
    @DeleteMapping("/deleteTeacher/{teacherId}")
    public R deleteTeacher(@ApiParam(value = "教师 id", required = true)
                           @PathVariable String teacherId) {
        if (teacherService.removeById(teacherId)) return R.ok().message("删除成功");
        return R.error().message("删除失败");
    }

    @ApiOperation("修改密码")
    @PutMapping("/updatePassword/{teacherId}/{newPassword}")
    public R updatePassword(@ApiParam(value = "教师 id", required = true)
                            @PathVariable String teacherId,
                            @ApiParam(value = "新密码", required = true)
                            @PathVariable String newPassword) {
        if (teacherService.updatePassword(teacherId, newPassword)) return R.ok().message("修改成功");
        return R.error().message("修改失败");
    }

    @ApiOperation("分页查询教师信息")
    @PostMapping("/getTeacherList/{page}/{limit}")
    public R getTeacherListByPage(@ApiParam(value = "当前页码", required = true)
                                  @PathVariable String page,
                                  @ApiParam(value = "每条记录数", required = true)
                                  @PathVariable String limit,
                                  @ApiParam(value = "教师模糊查询对象", required = true)
                                  @RequestBody
                                  TeacherQueryVo teacherQueryVo) {
        System.out.println(teacherQueryVo);
        Page<TeacherVo> pageModel = teacherService.getTeacherVoListByPage(Long.parseLong(page), Long.parseLong(limit), teacherQueryVo);
        return R.ok()
                .data("total", pageModel.getTotal())
                .data("size", pageModel.getSize())
                .data("teacherList", pageModel.getRecords())
                .message("获取数据成功");
    }


    @ApiOperation("根据 token 判断该教师是否拥有该权限")
    @GetMapping("/checkAuth/{rightId}")
    public R checkAuth(@ApiParam(value = "权限 Id", required = true)
                       @PathVariable String rightId,
                       HttpServletRequest request) {
        if (teacherService.checkAuth(request,rightId)) return R.ok();
        return R.error().message("抱歉，您尚未拥有该权限");
    }
}

